/*
 * Created on 2007-2-18
 *
 * TODO 
 */
package jacky.lanlan.song.extension.struts.action;

import org.apache.struts.validator.LazyValidatorForm;
import org.junit.Test;

/**
 * @author Jacky.Song
 *
 * 测试POJOAction。
 */
public class POJOTestActionTest_MockStruts extends BaseStrutsActionTest {

	@Override
	protected String getConfigLocation() {
		return "/config/struts/struts-config.xml";
	}
	
	@Test
	public void testInvokeBusinessMethod() throws Exception {
		addRequestParameter("param", "value_reqParam");
		addRequestParameter("aaa", "string value");
		addRequestParameter("bbb", "111");
		
		setRequestPathInfo("/POJOTest!business_method");

		actionPerform();
    
		verifyForward("success");
    verifyForwardPath("/successTo.jsp");
    
    verifyNoActionErrors();
    
    assertEquals("out_request", getRequest().getAttribute("ccc"));
    assertEquals("out_session", getSession().getAttribute("ddd"));
    
	}
	
	@Test
	public void testFormValidateError() throws Exception {
		addRequestParameter("param", "value_reqParam");
		
		setRequestPathInfo("/POJOTest!form_validate_error");
		
		TestFormBean form=new TestFormBean();//这个FormBean的validate()会返回非空的ActionErrors
		setActionForm(form);
		
		actionPerform();

		verifyInputForward();
		verifyForwardPath("/login.jsp");
		
		verifyActionErrors(new String[]{"aaa.bbb.ccc"});
    
	}
	
	@Test
	public void testInvokeBusinessMethodInOtherPack() throws Exception {
		setRequestPathInfo("/POJOTestActionInOtherPack!execution_business_method");
		addRequestParameter("param", "value_reqParam");
		
		LazyValidatorForm form=registerActionForm(LazyValidatorForm.class);		
		form.set("aaa", "string value");
		form.set("bbb", "111");
		
		actionPerform();
		
		verifyForward("success");
		verifyForwardPath("/successTo.jsp");
		
		verifyNoActionErrors();
		
		assertEquals("out_request", getRequest().getAttribute("ccc"));
		assertEquals("out_session", getSession().getAttribute("ddd"));
		
	}
	
	@Test
	public void testFormValidateErrorInOtherPack() throws Exception {
		addRequestParameter("param", "value_reqParam");
		
		setRequestPathInfo("/POJOTestActionInOtherPack!form_validate_error");
		
		TestFormBean form=new TestFormBean();//这个FormBean的validate()会返回非空的ActionErrors
		setActionForm(form);
		
		actionPerform();
		
		verifyInputForward();
		verifyForwardPath("/login.jsp");
		
		verifyActionErrors(new String[]{"aaa.bbb.ccc"});
		
	}
	
	@Test
	public void testExecuteMethodWithDomain() throws Exception {
		setRequestPathInfo("/POJOTest!execute_method_with_domain");
		
		addRequestParameter("param", "value_reqParam");

		LazyValidatorForm form=registerActionForm(LazyValidatorForm.class);		
		form.set("carNo", "川A8888");
		form.set("installNum", "111");
		form.set("emitTime", "2008-2-11");
		
		actionPerform();

		verifyForward("success");
		verifyForwardPath("/AAA.jsp");
		
		verifyNoActionErrors();
	}
	
	@Test
	public void testCacelled() throws Exception {
		getRequest().setAttribute("org.apache.struts.action.CANCEL", "true");
		//
		setRequestPathInfo("/POJOTest!business_method");//调用取消方法
		actionPerform();
		
		verifyInputForward();
		verifyForwardPath("/login.jsp");
		
	}
	
	@Override
	protected void onTearDown() {
		super.onTearDown();
	}
	
}
